package com.coco.goods.controller;

import com.coco.common.annotation.IngoreResponseAdvice;
import com.coco.common.exception.CustomException;
import com.coco.common.exception.ErrorEnum;
import com.coco.common.result.ResultData;
import com.coco.goods.vo.GoodsSearchVo;
import com.coco.goods.pojo.TbGoods;
import com.coco.goods.service.GoodsService;
import com.coco.goods.vo.GoodsVo;
import io.swagger.annotations.Api;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

/**
 * controller
 *
 * @Author: guofeng
 * @Date: 2021/07/14/20:46
 * @Description:
 */
@Api
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 增加
     *
     * @param goodsVo
     * @return
     */
    @RequestMapping("/addGoods")
    public void addGoods(@RequestBody GoodsVo goodsVo){
        try {
            goodsService.addGoods(goodsVo);
        } catch (Exception e) {
            throw new CustomException(ErrorEnum.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public void delete(String [] ids){
        try {
            goodsService.delete(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改
     *
     * @param goodsVo
     * @return
     */
    @RequestMapping("/update/{id}")
    public void update(@RequestBody GoodsVo goodsVo,@PathVariable("id") String id){
        try {
            goodsVo.getGoods().setId(id);
            goodsService.update(goodsVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取实体
     *
     * @param id
     * @return
     */
    @GetMapping("/findOne/{id}")
    public GoodsVo findOne(@PathVariable("id") String id){
       return goodsService.findOne(id);
    }

    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findAll")
    public List<TbGoods> findAll(){
       return goodsService.findAll();
    }

    /**
     *@描述 出售中商品列表
     *@参数  [page, size]
     *@返回值  java.util.List<com.coco.goods.pojo.TbGoods>
     *@创建人  zhanglongfei
     *@创建时间  2021/7/15
     *@修改人和其它信息
     */
    @RequestMapping("/saleList")
    public ResultData saleList(@RequestBody GoodsSearchVo goodsSearchVo){
        return goodsService.saleList(goodsSearchVo);
    }

    /**
     *@描述   加入回收站
     *@参数  [id] 需要传入 对应商品的id
     *@返回值  java.lang.Boolean
     *@创建人  zhanglongfei
     *@创建时间  2021/7/15
     *@修改人和其它信息
     */
    @RequestMapping("/addRecycle/{id}")
    public void addRecycle(@PathVariable String id){
        goodsService.addRecycle(id);
    }


    /**
     * 返回全部列表带条件
     *
     * @return
     */
    @RequestMapping("/findAllSearch")
    public List<TbGoods> findAll(@RequestBody TbGoods goods){
        return goodsService.findAll(goods);
    }

    /**
     * 查询+分页
     *
     * @param goods
     * @return
     */
    @RequestMapping("/search")
    public List<TbGoods> search(@RequestBody TbGoods goods){
       return goodsService.findAllSearch(goods);
    }

    /**
     *@描述  上下架
     *@参数  [id, status]
     *@返回值  boolean
     *@创建人  zhanglongfei
     *@创建时间  2021/7/15
     *@修改人和其它信息
     */
    @GetMapping("/putAway/{id}/{status}")
    public boolean putAway(@PathVariable String id,@PathVariable Integer status){
        return goodsService.putAway(id,status);
    }
    /**
     * 售空列表
     *
     * @param goodsSearchVo
     * @return
     * @Author: Lin
     */
    @RequestMapping("/findSelloutGoods")
    public ResultData findSelloutGoods(@RequestBody GoodsSearchVo goodsSearchVo){
        return goodsService.findSelloutGoods(goodsSearchVo);
    }

    /**
     * 回收站列表
     *
     * @param goodsSearchVo
     * @return
     * @Author: Lin
     */
    @RequestMapping("/findRecycleGoods")
    public ResultData findRecycleGoods(@RequestBody GoodsSearchVo goodsSearchVo){
        return goodsService.findRecycleGoods(goodsSearchVo);
    }

    /**
     * 回收站商品恢复
     *
     * @param id
     * @Author: Lin
     */
    @RequestMapping("/recoverRecycleGoods/{id}")
    public void recoverRecycleGoods(@PathVariable String id) {
        goodsService.recoverRecycleGoods(id);
    }

    /**
     *@描述   仓库中列表
     *@参数  [goodsSearchVo]
     *@返回值  com.coco.common.result.ResultData
     *@创建人  zhanglongfei
     *@创建时间  2021/7/15
     *@修改人和其它信息
     */
    @RequestMapping("/warehouseList")
    public ResultData warehouseList(@RequestBody GoodsSearchVo goodsSearchVo){
        return goodsService.warehouseList(goodsSearchVo);
    }
    /**
     * 删除回收站商品
     *
     * @param id
     * @Author: Lin
     */
    @RequestMapping("/deleteRecycleGoods/{id}")
    public void deleteRecycleGoods(@PathVariable String id) {
        goodsService.deleteRecycleGoods(id);
    }


    @GetMapping("/findSkuBySpuIdAndSkuId/{spuId}/{skuId}")
    @IngoreResponseAdvice
    public Object findSkuBySpuIdAndSkuId(@PathVariable("spuId") Long spuId,@PathVariable("skuId") Long skuId){

        return goodsService.findSkuBySpuIdAndSkuId(spuId, skuId);
    }

    @GetMapping("/findOneById/{id}")
    @IngoreResponseAdvice
    public TbGoods findOneById(@PathVariable("id") String id){
        return goodsService.findOneById(id);
    }

}



